<?php

namespace app\data\controller\api\auth;

use app\data\controller\api\Auth;
use app\data\service\UserTransferService;

/**
 * 用户余额转账
 * Class Balance
 * @package app\data\controller\api\auth
 */
class Balance extends Auth
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'DataUserBalance';

    /**
     * 账户明细记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $query = $this->_query($this->table);
        $query->withoutField('deleted,create_by')->where(['uuid' => $this->uuid, 'deleted' => 0]);
        $result = $query->order('id desc')->page(true, false, false, 10);
        $this->success('获取账户明细成功', $result);
    }

    /**
     * 我的钱包
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        $money = $this->app->db->name('data_user')->where(['id' => $this->uuid])->value('money');

        $totalAmount = $this->app->db->name($this->table)
            ->where(['uuid' => $this->uuid, 'deleted' => 0])
            ->whereIn('type', ['reward', 'system'])
            ->sum('amount');

        $todayAmount = $this->app->db->name($this->table)
            ->where(['uuid' => $this->uuid, 'deleted' => 0])
            ->whereIn('type', ['reward', 'system'])
            ->whereBetween('create_at', [date('Y-m-d') . ' 00:00:00', date('Y-m-d') . ' 23:59:59'])
            ->sum('amount');

        $this->success('我的钱包', [
            'money' => $money,
            'total_amount' => number_format($totalAmount, 2, '.', ''),
            'today_amount' => number_format($todayAmount, 2, '.', ''),
        ]);
    }

    /**
     * 获取实际到账金额
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function actualReceipt()
    {
        $data = $this->_vali([
            'amount.require'    => '提现转账金额不能为空',
        ]);

        $chargeRate = floatval(UserTransferService::instance()->config('charge'));

        $this->success('获取实际到账金额', [
            'actual_receipt' => bcmul($data['amount'], (bcsub(1, (bcdiv($chargeRate, 100, 4)), 4)), 2)
        ]);
    }
}